package com.jiyi.fls_flutter.flsplugin;


import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jiyi.jy_flssdk.JYFLSManager;
import com.jiyi.jy_flssdk.listener.JYFLSEventListener;
import com.jiyi.jy_flssdk.listener.JYFLSInitListener;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;

/** FlspluginPlugin */
public class FlspluginPlugin implements FlutterPlugin, MethodCallHandler {
  /// The MethodChannel that will the communication between Flutter and native Android
  ///
  /// This local reference serves to register the plugin with the Flutter Engine and unregister it
  /// when the Flutter Engine is detached from the Activity
  private MethodChannel channel;
  private Context mContext;
  static final String TAG = "JY_FLS";

  @Override
  public void onAttachedToEngine( FlutterPluginBinding flutterPluginBinding) {
    channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "flsplugin");
    channel.setMethodCallHandler(this);

    mContext = flutterPluginBinding.getApplicationContext();
  }

  @Override
  public void onMethodCall( MethodCall call,  Result result) {
    if (call.method.equals("getPlatformVersion")) {
      result.success("Android " + android.os.Build.VERSION.RELEASE);
    }else if (call.method.equals("fls_init")){
      fls_init(call, result);
    }else if (call.method.equals("fls_start")) {
      fls_start(call, result);
    }else {
      result.notImplemented();
    }
  }

  //  FLS的方法
  public void fls_init(MethodCall call, final Result result) {
    Log.d(TAG, "调用了方法:fls_init");
    HashMap<String, Object> options = call.arguments();

    if (!options.containsKey("channel_id")) {
      return;
    }

    String ch = (String) options.get("channel_id");
    JYFLSManager.getInstance().initSDK(ch, true, mContext, new JYFLSInitListener() {
      @Override
      public void getInitStatus(int i, String s, HashMap<String, String> hashMap) {
        Log.d(TAG, s);
        final JSONObject data = new JSONObject();
        data.put("code", i);
        data.put("msg", s);
        data.put("data", hashMap);
        Handler handler = new Handler(Looper.getMainLooper());
        handler.post(new Runnable() {
          @Override
          public void run() {
            result.success(data) ;
          }
        });
      }

      @Override
      public void getInitStatus(int i, String s) {

      }
    });
  }

  public void fls_start(MethodCall call, final Result result) {
    Log.d(TAG, "调用了方法:fls_start");
    HashMap<String, Object> options = call.arguments();

    if (!options.containsKey("user_id")) {
      return;
    }

    final String userId = (String)options.get("user_id");
    new Thread(new Runnable() {
      @Override
      public void run() {
        JYFLSManager.getInstance().startFLSWithUid(userId, new JYFLSEventListener() {
          @Override
          public void eventListener(int i, String s, HashMap<String, String> hashMap) {
            final JSONObject data = new JSONObject();
            data.put("code", i);
            data.put("msg", s);
            data.put("data", hashMap);
            result.success(data);
          }
        });
      }
    }).start();

  }

  @Override
  public void onDetachedFromEngine( FlutterPluginBinding binding) {
    channel.setMethodCallHandler(null);
  }
}
